package com.lc.demo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lc.demo.bean.*;
import com.lc.demo.result.ResponseMessage;
import com.lc.demo.service.StudentService;
import com.lc.demo.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.*;


@CrossOrigin
@RestController
@RequestMapping("/stu")
public class StudentController {

    @Autowired
    private StudentService studentService;

    //登陆接口
    @PostMapping(value = "/login")
    public ResponseMessage login(@RequestBody Student student, HttpSession session) {
        Student stu = studentService.login(student.getUsername(), student.getPassword());
        if (stu != null) {
            return ResponseMessage.successWithMessage(student.getUsername());
        } else {
            return ResponseMessage.errorWithMessage("用户名或密码错误");
        }
    }


    //查询 签到列表
    @PostMapping(value = "/findSignList")
    public ResponseMessage findSignList(@RequestBody    Map<String,Object> map) {
        Student student=new Student();
        BeanUtil.copyProperties2Bean(map,student);
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        PageHelper.startPage(pageNum, pageSize);
        List<Signin> signList = studentService.findSignList(student);
        PageInfo<Signin> page = new PageInfo<>(signList);
        return ResponseMessage.successWithData(page);
    }


    //请假接口
    @PostMapping(value = "/leave")
    public ResponseMessage leave(@RequestBody Leavetable leavetable) {

        studentService.leave(leavetable);
        return ResponseMessage.success();
    }

    //签到接口
    @PostMapping(value = "/signin")
    public ResponseMessage signin(@RequestBody Signin signin) {
        return studentService.signin(signin);

    }
}
